# daily batch backup
# 1.get date
# 2.create script to compress to zip( over 2G size)
# 3.run script!
# 4.move to zip folder
# 5.delete before 7 days dmp files
# 

"""
    Updated History
    
    2. Updated by Allbory on Mar 2nd 2011
        * Added \n to every write() method.
        
    1. Updated by Allbory on Feb 26th 2011
        * try/catch exception was added.
        * Save log file.
        
"""

import datetime
import os

# get today and try to return as string
today = datetime.datetime.now()

print "Current year: %d" % today.year
print "Current year: %d" % today.month
print "Current year: %d" % today.day

month_str = str(today.month)
day_str = str(today.day)

# months manipulation
if len(month_str) == 1:
    month_str = "0" + month_str
else:
    month_str = month_str
  
# date manipulation
if len(day_str) == 1:
    day_str = "0" + day_str
else:
    day_str = day_str

today_str = str(today.year) + month_str + day_str
tm_str = str(today.year) + month_str

print today_str

flag = "30"

#Genrate log file
try:
    flag = "31"
    py_log = open(today_str+"_py.log",'a')
    flag = "32"    
    current_time = datetime.datetime.now()
    flag = "33"     
    py_log.write (str(current_time) + ">>part2>>BackupScript2 getting started...\n")


except:
    py_log.write (">>part2>>Exception occurred while open py.log file..." + flag + "\n")
    

try:
# run script
    flag = "34"
    py_log.write (str(current_time) + ">>part2>>Run BZS script...\n")
    flag = "35"
    exec( today_str+".bzs" )
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>BZS script is done...\n")
    
except as err:
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>An error occurred while BZS is running with message : " + err +"\n")


current_time = datetime.datetime.now()
py_log.write (str(current_time) + ">>part2>>Deleting old file logic getting started...\n")

# logic for delete old files
# -7 days
last7_date = datetime.datetime.now() + datetime.timedelta(days=-7)

last7mon_str = str(last7_date.month)
last7day_str = str(last7_date.day)

# months manipulation
if len(last7mon_str) == 1:
    last7mon_str = "0" + last7mon_str
else:
    last7mon_str = last7mon_str
  
# date manipulation
if len(last7day_str) == 1:
    last7day_str = "0" + last7day_str
else:
    last7day_str = last7day_str

last7day_fullstr = str(last7_date.year) + last7mon_str + last7day_str
last7mon_fullstr = str(last7_date.year) + last7mon_str

print last7day_fullstr
print last7mon_fullstr

current_time = datetime.datetime.now()
py_log.write (str(current_time) + ">>part2>>last7day_fullstr >> " + last7day_fullstr + "\n")
py_log.write (str(current_time) + ">>part2>>last7mon_fullstr >> " + last7mon_fullstr + "\n")

py_log.write (current_time + ">>part2>>deleting -7 files...\n")
#remove dmp, log, bzs files
try:
    os.remove(last7day_fullstr+".log")
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>log file has been deleted\n")    
except:
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>An error occurred while deleting log file\n")    

try:
    os.remove(last7day_fullstr+".dmp")
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>dmp file has been deleted\n")    
except:
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>An error occurred while deleting dmp file\n")
    
try:
    os.remove(last7day_fullstr+".bzs")
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>bzs file has been deleted\n")    
except:
    current_time = datetime.datetime.now()
    py_log.write (str(current_time) + ">>part2>>An error occurred while deleting bzs file\n")    

current_time = datetime.datetime.now()
py_log.write (str(current_time) + ">>part2>>BackupScript2 is done successfully...\n")        

py_log.close()

